﻿using System.Collections.Generic;
using System.Linq;
using Orchard;
using Orchard.ContentManagement;

namespace Piedone.ContentTemplates.Services
{
    public interface ITemplateService : IDependency
    {
        IEnumerable<ITemplateLoader> GetLoaders();
        IContentQuery GetTemplatesQuery();
    }

    public static class TemplateServiceExtensions
    {
        public static IContentQuery GetTemplatesQuery(this ITemplateService service, params string[] contentTypes)
        {
            return service.GetTemplatesQuery().ForPart<ContentPart>().ForType(contentTypes);
        }

        public static IEnumerable<ITemplateLoader> GetLoadersFor(this ITemplateService service, IContent item)
        {
            return service.GetLoaders().Where(loader => loader.CanHandle(item));
        }

        public static IEnumerable<ITemplateLoader> GetLoadersFor(this ITemplateService service, IContent template, IContent item)
        {
            return service.GetLoaders().Where(loader => loader.CanHandle(template) || loader.CanHandle(item));
        }
    }
}
